package com.ruoyi.client.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.client.domain.entity.BlogUser;
import com.ruoyi.client.mapper.BlogArticleMapper;
import com.ruoyi.client.mapper.BlogUserMapper;
import com.ruoyi.client.service.IBlogUserService;
import com.ruoyi.common.core.domain.bo.RegisterBody;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author fz
 * @since 2025-02-27
 */
@Service
public class BlogUserServiceImpl extends ServiceImpl<BlogUserMapper, BlogUser> implements IBlogUserService {
//    @Autowired
//    SysRegisterService registerService;
    @Autowired
    BlogArticleMapper articleMapper;
    @Override
    public TableDataInfo<BlogUser> selectBlogUserList(PageQuery pageQuery) {
        Page<BlogUser> page = this.page(pageQuery.build(),buildWrapper(pageQuery.getParams()));
        return TableDataInfo.build(page);
    }

    @Override
    @Transactional
    public void register(RegisterBody body) {
        String username = body.getUsername();
        String password = body.getPassword();
        String confirmPassword = body.getConfirmPassword();
        if (!password.equals(confirmPassword)) {
            throw new ServiceException("两次密码不匹配");
        }
        BlogUser blogUser = new BlogUser();
        blogUser.setUsername(username);
        blogUser.setNickname("用户" + UUID.fastUUID());
        blogUser.setPassword(BCrypt.hashpw(password));

//        registerService.registerByClient(body);
        save(blogUser);
    }

    @Override
    public BlogUser getInfo(Long id) {
        return articleMapper.getInfo(id);
    }

    private Wrapper<BlogUser> buildWrapper(Map<String, Object> params) {
        QueryWrapper<BlogUser> wrapper = new QueryWrapper<>();
        if (CollectionUtils.isEmpty(params)) return wrapper;
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            wrapper.between("create_time",beginTime,endTime);
            params.remove("beginTime");
            params.remove("endTime");
        }
        Set<Map.Entry<String, Object>> set = params.entrySet();
        for (Map.Entry<String, Object> entry : set) {
            String field = entry.getKey();
            Object value = entry.getValue();

            if (value != null) {
                // 添加等值条件
                wrapper.like(field,value);
            }
        }
        return wrapper;
    }
}
